package com.zhouyibike.uu.ui.activity.web;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.TextView;

import com.zhouyibike.uu.R;
import com.zhouyibike.uu.config.Config;
import com.zhouyibike.uu.entity.GetCodeResult;
import com.zhouyibike.uu.http.ApiCallback;
import com.zhouyibike.uu.ui.base.BaseActivity;

import java.util.HashMap;
import java.util.Map;

public class XieyiActivity extends BaseActivity implements View.OnClickListener{

    private WebView tv_xieyi;
    private ImageButton mBtnBack;
    private TextView mBtnTitle;

    private String url;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xieyi);
        initView();
        getWZMsg();
    }
    private void initView() {
        mBtnBack = (ImageButton) findViewById(R.id.bar_btn_back);
        mBtnTitle = (TextView) findViewById(R.id.bar_text_title);
        mBtnTitle.setText("用户注册协议");
        mBtnBack.setOnClickListener(this);
        tv_xieyi= (WebView) findViewById(R.id.tv_xieyi_msg);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bar_btn_back:
                finish();
                break;
        }
    }
    private void getWZMsg(){
        Map<String, String> params = new HashMap<>();
        params.put("token", preferences.getString("token",""));
        params.put("type", "4");
        showProgressDialog();
        addSubscription(apiStores.GetInformationzcxy(params),
                new ApiCallback<GetCodeResult>() {

                    @Override
                    public void onSuccess(GetCodeResult model) {
                        if(model.getStatus()== Config.SUCCESSCODE){
                            url=model.getData();
                            initWV();
                        }else{
                            toastShow(model.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        toastShow(msg);
                    }

                    @Override
                    public void onFinish() {

                    }
                });
    }
    private void initWV() {
        //设置支持JavaScript脚本
        WebSettings webSettings = tv_xieyi.getSettings();
        webSettings.setJavaScriptEnabled(true);
        //设置可以访问文件
        webSettings.setAllowFileAccess(true);
        //设置支持缩放
        webSettings.setBuiltInZoomControls(false);

        webSettings.setDatabaseEnabled(true);

        //使用localStorage则必须打开
        webSettings.setDomStorageEnabled(true);

        webSettings.setGeolocationEnabled(true);

        tv_xieyi.setWebChromeClient(new WebChromeClient() {

            @Override
            //设置网页加载的进度条
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress >= 100) {
                    dismissProgressDialog();
                }
                super.onProgressChanged(view, newProgress);
            }

        });
        tv_xieyi.loadUrl(url);
    }
}
